package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.StaffDao;
import com.swt.domain.Shop;
import com.swt.domain.Staff;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "TTStaffDaoImpl")
public class StaffDaoImpl implements StaffDao {
    @Resource
    private HibernateTemplate hibernateTemplate;


    @Override
    public boolean staffAdd(String staffname, String staffphone, String staffsex, String staffrole, String staffsupport, String staffimg, int shopid,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Shop shop = session.get(Shop.class,shopid);

        Staff staff = new Staff();

        staff.setStaffname(staffname);
        staff.setStaffphone(staffphone);
        staff.setStaffsex(staffsex);
        staff.setStaffrole(staffrole);
        staff.setStaffsupport(staffsupport);
        staff.setStaffimg(staffimg);
        staff.setShopid(shop);
        staff.setClubClubId(clubClubId);

        try {
            session.save(staff);
        }catch (Exception e){
            return false;
        }

        return true;

    }

    @Override
    public List<Staff> staffQuery(int page,String query, int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Staff where clubClubId = ? ";
        if (query.length()>0){
            sql = sql+"and staffname like '%"+query+"%' ";
        }
        System.out.println(sql);

        List<Staff> staffList = session.createQuery(sql).setParameter(0,clubClubId).setFirstResult(page).setMaxResults(9).list();
        return staffList;
    }
    //员工删除
    @Override
    public Boolean staffDelete(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Staff staff = session.get(Staff.class,id);

        try {
            session.delete(staff);
        }catch (Exception e){
            return false;
        }
        return true;

    }
    //通过id查询就行修改操作
    @Override
    public List<Staff> staffQuery2(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Staff where id = ?";
        Query query = session.createQuery(sql);
        List<Staff> staffList = query.setParameter(0,id).list();

        return staffList;

    }

    @Override
    public Boolean staffUpdate(int staffid,String staffname, String staffphone, String staffsex, String staffrole, String staffsupport, String staffimg, int shopid) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Shop shop = session.get(Shop.class,shopid);

        Staff staff = new Staff();
        staff.setStaffid(staffid);
        staff.setStaffname(staffname);
        staff.setStaffphone(staffphone);
        staff.setStaffsex(staffsex);
        staff.setStaffrole(staffrole);
        staff.setStaffsupport(staffsupport);
        staff.setStaffimg(staffimg);
        staff.setShopid(shop);

        try {
            session.update(staff);
        }catch (Exception e){
            return false;
        }

        return true;
    }

//    排课页面查询教练
    @Override
    public List<Staff> queryStaffPK(int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Staff where staffrole = ? and clubClubId = ?";
        Query query = session.createQuery(sql);
        List<Staff> staffList = query.setParameter(0,"教练").setParameter(1,clubClubId).list();

        return staffList;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
